package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.ExceptionResult;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.service.ICarouselService;
import com.example.entity.Carousel;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengc
 * @since 2023-05-19
 */
@RestController
@RequestMapping("/carousel")
public class CarouselController {

        @Resource
        private ICarouselService carouselService;

        //新增与修改同一个方法
        @PostMapping
        @CacheEvict(cacheNames = "carousel",key = "targetClass",beforeInvocation = false)
        public ExceptionResult save(@RequestBody Carousel carousel) {
            return ExceptionResult.success(carouselService.saveOrUpdate(carousel));
        }

        //删除
        @DeleteMapping("/{id}")
        public ExceptionResult delete(@PathVariable Integer id) {
            return ExceptionResult.success(carouselService.removeById(id));
        }

        //查询列表
        @GetMapping
        @Cacheable(cacheNames = "carousel",key = "targetClass")
        public ExceptionResult findAll() {
            return ExceptionResult.success(carouselService.list());
        }

        //根据id查询单个
        @GetMapping("/{id}")
        public ExceptionResult findOne(@PathVariable Integer id) {
            return ExceptionResult.success(carouselService.getById(id));
        }

        //批量删除
        @PostMapping("/del/batch")
        public ExceptionResult deleteBatch(@RequestBody List<Integer> ids){
                return ExceptionResult.success(carouselService.removeByIds(ids));
        }

        //分页查询
        @GetMapping("/page")
        public ExceptionResult findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
                QueryWrapper<Carousel> queryWrapper=new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                return ExceptionResult.success(carouselService.page(new Page<>(pageNum, pageSize),queryWrapper));
        }
}
